package com.kitty.game.skill;


import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.skill.model.RoleSkill;
import com.kitty.game.config.Skill;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.skill.message.ReqLearnSkill;
import com.kitty.game.skill.service.SkillService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.StudySkillEvent;
import com.kitty.common.model.Reason;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.role.service.RoleService;
import org.apache.mina.core.session.IoSession;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

@Controller
public class SkillController {
    Logger logger = LoggerFactory.getLogger(SkillController.class);

    /**
     * 学习技能
     *
     * @param session
     * @param reqLearnSkill
     */
    @RequestMapping
    public void learnSkill(IoSession session, ReqLearnSkill reqLearnSkill) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }

        if (reqLearnSkill.getRoleId() != role.getRoleId()) {
            /**学习技能的不是人物时，判断是否是宠物*/
            Pet pet = SpringUtils.getPetService().getPetById(reqLearnSkill.getRoleId(), role);
            if (pet != null) {
                SpringUtils.getPetService().learnPetSkill(role, pet, reqLearnSkill.getSkillId(), reqLearnSkill.getLearnLevel());
            }
        } else {
            learnRoleSkill(session, reqLearnSkill, role);
        }
    }

    private void learnRoleSkill(IoSession session, ReqLearnSkill reqLearnSkill, Role role) {
        if (reqLearnSkill.getLearnLevel() > 10 || reqLearnSkill.getLearnLevel() < 0) { //如果要学习的技能等级超过10 或者小于0
            return;
        }
        logger.info("学习技能 ====={}", Json.toJson(reqLearnSkill));
        if (role == null || reqLearnSkill.getRoleId() != role.getRoleId()) {//学习技能的不是人物
            return;
        }
        SkillService skillService = SpringUtils.getBean(SkillService.class);
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        Skill skill = skillService.getSkillInfo(reqLearnSkill.getSkillId());
        if (skill == null) {
            return;//没有技能信息
        }

        if (skill.getSkillJob() != role.getPolar() && !skill.getSkillName().equals("力破千钧") && !skill.getSkillName().equals("修道术") && !skill.getSkillName().equals("神体术")) {//不是自己门派的技能或者不是力魄
            return;
        }
        RespMsg respMsg = new RespMsg();
        if ((skill.getJeishu() == 1 || skill.getJeishu() == 4 || skill.getJeishu() == 5) && skill.getReqLevel() > role.getLevel()) {
            respMsg.setMsg("等级不足，无法学习。");
            MessagePusher.pushMessage(session, respMsg);
            return;
        }

        RoleSkill roleSkill = skillService.getRoleSkill(role, reqLearnSkill.getSkillId());
        RoleSkill frontSkill = skillService.getRoleSkill(role, skill.getFrontSkill());

        if (frontSkill != null) {
            if (frontSkill.getLevel() < skill.getSkillFront()) {
                respMsg.setMsg("#R" + frontSkill.getName() + "#n技能等级不足，无法学习。");
                MessagePusher.pushMessage(session, respMsg);
                return;
            }
        } else {
            if (skill.getJeishu() != 1 && !skill.getSkillName().equals("力破千钧") && !skill.getSkillName().equals("修道术") && !skill.getSkillName().equals("神体术")) {//如果不是一阶技能并且不是力破 return
                return;
            }
        }

        if (roleSkill == null) {
            if (skill.getSkillLife() == 0) {//用潜能学
                int needPot = skillService.getLearnPotNew(0, reqLearnSkill.getLearnLevel(), skill.getSkillType(), skill.getJeishu());
                if (role.getPot() >= needPot) {
                    role.setPot(role.getPot() - needPot);
                    roleService.updateRolePot(role);
                    roleSkill = new RoleSkill();
                    roleSkill.setName(skill.getSkillName());
                    roleSkill.setRoleId(role.getRoleId());
                    roleSkill.setType((short) skill.getSkillType());
                    roleSkill.setSkillId(reqLearnSkill.getSkillId());
                    roleSkill.setSkillJieshu(skill.getJeishu());
                    roleSkill.setLevel(reqLearnSkill.getLearnLevel());
                    roleSkill.setMana(skillService.getSkillMana(roleSkill));
                    roleSkill.setTargetCount((short) skillService.getSkillTargetNum(roleSkill));
                    skillService.learn(role, roleSkill);
                } else {
                    respMsg.setMsg("潜能不足。");
                    MessagePusher.pushMessage(session, respMsg);
                    return;
                }
                MessagePusher.pushMessage(session, new RespNotifyMiscEx("你花费#R" + needPot + "#n点潜能将#R" + roleSkill.getName() + "#n的等级提升到了#R" + roleSkill.getLevel() + "#n级！"));
            } else {//用金钱
                int needMoney = skillService.getLearnCash(0, reqLearnSkill.getLearnLevel());
                int DBMoney = role.getMoney();
                if (DBMoney >= needMoney) {
                    roleService.subtractMoney(role, needMoney, Reason.BUY_ITEM);
                    roleService.updateRoleMoney(role);
                    roleSkill = new RoleSkill();
                    roleSkill.setName(skill.getSkillName());
                    roleSkill.setRoleId(role.getRoleId());
                    roleSkill.setType((short) skill.getSkillType());
                    roleSkill.setSkillId(reqLearnSkill.getSkillId());
                    roleSkill.setSkillJieshu(skill.getJeishu());
                    roleSkill.setLevel(reqLearnSkill.getLearnLevel());
                    roleSkill.setMana(skillService.getSkillMana(roleSkill));
                    roleSkill.setTargetCount((short) skillService.getSkillTargetNum(roleSkill));
                    skillService.learn(role, roleSkill);
                } else {
                    respMsg.setMsg("你的金钱不足！请到积分商城购买");
                    MessagePusher.pushMessage(session, respMsg);
                    return;
                }
                MessagePusher.pushMessage(session, new RespNotifyMiscEx("你花费" + AsktaoUtil.getMoneyFormat(needMoney) + "文金钱将#R" + roleSkill.getName() + "#n的等级提升到了#R" + roleSkill.getLevel() + "#n级！"));
            }

        } else {
            if (new Double(role.getLevel() * 1.6).intValue() < roleSkill.getLevel() + reqLearnSkill.getLearnLevel()) {
                reqLearnSkill.setLearnLevel((short) ((new Double(role.getLevel() * 1.6).intValue()) - roleSkill.getLevel()));
            }
            if (roleSkill.getLevel() + reqLearnSkill.getLearnLevel() > new Double(role.getLevel() * 1.6).intValue()) {//学习等级超过人物最大能学的等级
                return;
            }
            if (skill.getSkillLife() == 0) {//用潜能学
                int needPot = skillService.getLearnPotNew(roleSkill.getLevel(), roleSkill.getLevel() + reqLearnSkill.getLearnLevel(), skill.getSkillType(), skill.getJeishu());
                if (role.getPot() >= needPot) {
                    role.setPot(role.getPot() - needPot);
                    roleSkill.setLevel((short) (roleSkill.getLevel() + reqLearnSkill.getLearnLevel()));
                    roleSkill.setMana(skillService.getSkillMana(roleSkill));
                    roleSkill.setTargetCount((short) skillService.getSkillTargetNum(roleSkill));
                    role.save();
                    roleService.updateRolePot(role);
                } else {
                    respMsg.setMsg("潜能不足。");
                    MessagePusher.pushMessage(session, respMsg);
                    return;
                }
                MessagePusher.pushMessage(session, new RespNotifyMiscEx("你花费#R" + needPot + "#n点潜能将#R" + roleSkill.getName() + "#n的等级提升到了#R" + roleSkill.getLevel() + "#n级！"));
            } else {
                int needMoney = skillService.getLearnCash(roleSkill.getLevel(), reqLearnSkill.getLearnLevel());
                int DBMoney = role.getMoney();
                if (DBMoney >= needMoney) {
                    roleService.subtractMoney(role, needMoney, Reason.BUY_ITEM);
                    roleSkill.setLevel((short) (roleSkill.getLevel() + reqLearnSkill.getLearnLevel()));
                    roleSkill.setMana(skillService.getSkillMana(roleSkill));
                    roleSkill.setTargetCount((short) skillService.getSkillTargetNum(roleSkill));
                    role.save();
                    roleService.updateRoleMoney(role);
                } else {
                    respMsg.setMsg("你的金钱不足！请到积分商城购买");
                    MessagePusher.pushMessage(session, respMsg);
                    return;
                }
                MessagePusher.pushMessage(session, new RespNotifyMiscEx("你花费" + AsktaoUtil.getMoneyFormat(needMoney) + "文钱将#R" + roleSkill.getName() + "#n的等级提升到了#R" + roleSkill.getLevel() + "#n级！"));
            }
        }

        /**产生一个学习了技能的事件*/
        EventDispatcher.getInstance().fireEvent(new StudySkillEvent(EventType.STUDY_SKILL, role, roleSkill.getSkillId(), roleSkill.getLevel()));
        /**发送增益属性*/
        SpringUtils.getBean(RoleService.class).sendExtraAttribute(role);

    }
}
